package neusoft.edu.cn.guanshangshiguang.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.picasso.Picasso;

import java.util.List;

import neusoft.edu.cn.guanshangshiguang.Listener.OnItemClickListener;
import neusoft.edu.cn.guanshangshiguang.R;
import neusoft.edu.cn.guanshangshiguang.bean.OnlineBookListBean;
import neusoft.edu.cn.guanshangshiguang.bean.PersonalBookListBean;
import neusoft.edu.cn.guanshangshiguang.common.Common;
import neusoft.edu.cn.guanshangshiguang.item.BookDetailsItemViewHolder;

public class PersonalBookListAdapter extends RecyclerView.Adapter<BookDetailsItemViewHolder> {
    private List<PersonalBookListBean> personalBookListBeanList;
    private OnItemClickListener myOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener){
        myOnItemClickListener=listener;
    }

    public void setDate(List<PersonalBookListBean> dateItemList){
        this.personalBookListBeanList =dateItemList;
        notifyDataSetChanged();//通知数据集发生变化
    }

    @NonNull
    @Override
    public BookDetailsItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
        View itemView = layoutInflater.inflate(R.layout.book_details_item_layout,viewGroup,false);
        BookDetailsItemViewHolder viewHolder = new BookDetailsItemViewHolder(itemView);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(@NonNull BookDetailsItemViewHolder bookDetailsItemViewHolder, final int i) {
        final PersonalBookListBean dataBean = personalBookListBeanList.get(i);

        bookDetailsItemViewHolder.bookName.setText(dataBean.getBname());
        bookDetailsItemViewHolder.jianjie.setText("\u3000"+dataBean.getIntroduction());
        bookDetailsItemViewHolder.zuozhe.setText(dataBean.getAuthor());

        Picasso.get()
                .load(Common.Address+dataBean.getBpic())
                .into(bookDetailsItemViewHolder.bookPic);

        if(myOnItemClickListener!=null){
            bookDetailsItemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    myOnItemClickListener.onItemClick(view,i);
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        if(personalBookListBeanList !=null)
            return personalBookListBeanList.size();
        else
            return 0;
//        return shopDateList.size();//返回列表显示的数据个数
    }
}
